package com.example.shopp_2301.mine

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.example.base.mvvm.BaseFragment
import com.example.shopp_2301.R
import com.example.shopp_2301.adapter.VpAdapter
import com.example.shopp_2301.databinding.FragmentMineBinding
import com.example.shopp_2301.login.LoginViewModel
import com.example.shopp_2301.minefg.Order1Fragment
import com.example.shopp_2301.minefg.Order2Fragment
import com.example.shopp_2301.minefg.Order3Fragment
import com.example.shopp_2301.minefg.Order4Fragment
import com.example.shopp_2301.utils.GlideUtils
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector


class MineFragment : BaseFragment<FragmentMineBinding,LoginViewModel>() {
    lateinit var vpAdapter: VpAdapter
    val list = mutableListOf<Fragment>()
    override fun initView() {
        viewModel.user()
        //选项选择器
        binding.userName.setOnClickListener {
            val names = mutableListOf("张三","李四","王五")
            val optionPicker = OptionPicker(requireActivity())
            optionPicker.setData(names)
            optionPicker.setTitle("请选择昵称")
            optionPicker.setOnOptionPickedListener { position, item ->
                binding.userName.text = names[position]
            }
            optionPicker.show()
        }
        //日期选择器
        binding.userBirth.setOnClickListener {
            val datePicker = DatePicker(requireActivity())
            datePicker.setTitle("请选择出生年月日")
            datePicker.wheelLayout.setRange(DateEntity.target(1900,1,1),DateEntity.today(),
                DateEntity.today())//设置日期范围及默认选中时间
            datePicker.setOnDatePickedListener { year, month, day ->
                binding.userBirth.text = "$year-$month-$day"
            }
            datePicker.show()
        }
        //地址选择器
        binding.tvAddress.setOnClickListener {
            val addressPicker = AddressPicker(requireActivity())
            addressPicker.setTitle("请选择配送地址")
            addressPicker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)
            addressPicker.setOnAddressPickedListener { province, city, county ->
                binding.tvAddress.text = "${province.name}${city.name}${county.name}"
            }
            addressPicker.show()
        }
        //变更头像
        binding.userImage.setOnClickListener {
            PictureSelector.create(this,102).selectPicture()
        }
        //点击退出// 退出接口 （有） 环信im//将保存的token 清空
        binding.logout.setOnClickListener {
            SPUtils.getInstance().clear()
            ARouter.getInstance().build("/app/LoginCodeActivity").navigation()
        }
        //订单列表展示
        list.add(Order1Fragment())
        list.add(Order2Fragment())
        list.add(Order3Fragment())
        list.add(Order4Fragment())
        vpAdapter = VpAdapter(requireActivity(),list)
        binding.mineVp.adapter = vpAdapter
        //点击上方bottom
        binding.mineBottom.setOnNavigationItemSelectedListener(object :BottomNavigationView.OnNavigationItemSelectedListener{
            override fun onNavigationItemSelected(item: MenuItem): Boolean {
                when(item.itemId){
                    R.id.order1 -> binding.mineVp.currentItem = 0
                    R.id.order2 -> binding.mineVp.currentItem = 1
                    R.id.order3 -> binding.mineVp.currentItem = 2
                    R.id.order4 -> binding.mineVp.currentItem = 3
                }
                return true
            }
        })
        //滑动vp
        binding.mineVp.registerOnPageChangeCallback(object:ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when (position){
                    0->binding.mineBottom.selectedItemId = R.id.order1
                    1->binding.mineBottom.selectedItemId = R.id.order2
                    2->binding.mineBottom.selectedItemId = R.id.order3
                    3->binding.mineBottom.selectedItemId = R.id.order4
                }
            }
        })
    }

    override fun initData() {
        viewModel.userSuccess.observe(this){
            if(it.code==200){
                GlideUtils.loadCircleImage(requireActivity(),it.data.avatar,binding.userImage)
                binding.userName.text = it.data.nickname
            }

        }
    }

    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.fragment_mine
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==102&&resultCode == Activity.RESULT_OK){
            val bean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideUtils.loadCircleImage(requireActivity(),bean.path.toString(),binding.userImage)
        }
    }
}